/**
 * @file goods-detail.js
 * @author swan
 */
const app = getApp()
var bdParse = require('../../../utils/bdParse/bdParse.js')
var util = require('../../../utils/util.js')

Page({
    data: {
        host: getApp().getUrl(),
        index: 0,
        array: [1, 2, 3, 4, 5],
        showShopPopup: false, // 是否显示弹出层
        cur: 1, // 当前
        details_index: 1,
        buy_num: 1,
        goods_id: '',
        total_amount: 0
    },
    onLoad: function (option) {
        swan.showToast({
            title: '正在加载...',
            icon: 'loading',
            duration: 10000,
            mask: true
        });
        var that = this;
        swan.request({
            url: getApp().getUrl('/v1/goods-info'),
            data: {
                id: option.id
            },
            success: res => {
                swan.hideToast();
                let data = res.data.data;
                that.setData(data);
                that.setData({
                    sales_price: data.appleGoods.sales_price,
                    goods_id: data.appleGoods.id,
                    total_amount: data.appleGoods.sales_price * 1
                })
                that.data.appleGoods.introduction = bdParse.bdParse('introduction', 'html', data.appleGoods.introduction, that, 5)
                that.data.appleGoods.attention_app = bdParse.bdParse('attention_app', 'html', data.appleGoods.attention_app, that, 5)
            },
            fail: err => {
                swan.hideToast();
                swan.showToast({
                    title: '商品展示失败',
                    icon: 'none'
                });
            }
        });
    },
    onPaymentTap(e) {
        let goodsId = this.data.goods_id,
            buy_num = this.data.buy_num,
            openID = getApp().getOpenID();
        if (!openID) {
            // 没有openID，说明只是手百登录，但是还没有与三方服务器关联，需要先获取openID
            getApp().loginThenDoEvent(getApp().forceGetUserInfoThenDecrypt);
            return;
        }
        swan.showToast({
            title: '正在加载...',
            icon: 'loading',
            duration: 10000,
            mask: true
        });
        // 创建订单
        swan.request({
            url: getApp().getUrl('/v1/order'),
            data: {
                goods_id: goodsId,
                buy_num: buy_num,
                open_id: openID
            },
            method: 'POST',
            success: res => {
                swan.hideToast();
                let data = res.data;
                if (data.status != 1) {
                    swan.showToast({
                        title: data.message,
                        icon: 'none'
                    });
                    return false;
                }
                swan.showToast({
                    title: '支付成功',
                    icon: 'success'
                });
            },
            fail: err => {
                swan.hideToast();
                swan.showToast({
                    title: '订单创建失败',
                    icon: 'none'
                });
            }
        });

    },
    onDetailsTap(e) {
        let id = e.target.dataset.cur;
        console.log(id);
        this.setData({
            cur: id,
            details_index: id
        });
    },
    selectorChange(e) {
        console.log(e);
        let index = e.detail.value
        this.setData({
            index: index,
            buy_num: this.data.array[index],
            total_amount: util.accMul(this.data.sales_price, this.data.array[index])
        });
    },
    subBuy() {
        this.showModal();
    },
    showModal() {
        var animation = swan.createAnimation({
            duration: 500,
            timingFunction: 'linear',
            delay: 0,
            transformOrigin: "50% 50%",
        })
        this.animation = animation
        animation.translateY(200).step()
        this.setData({
            animationData: animation.export(), // export 方法每次调用后会清掉之前的动画操作。
            showShopPopup: true
        })
        setTimeout(() => {
            animation.translateY(0).step()
            this.setData({
                animationData: animation.export()  // export 方法每次调用后会清掉之前的动画操作。
            })
        }, 200)
    },
    hideModal() {
        var that = this;
        var animation = swan.createAnimation({
            duration: 1000,
            timingFunction: 'linear'
        })
        that.animation = animation
        animation.translateY(200).step()
        that.setData({
            animationData: animation.export()
        })
        setTimeout(function () {
            animation.translateY(0).step()
            that.setData({
                animationData: animation.export(),
                showShopPopup: false
            })
        }, 200)
    },
});
